John Davidson

Redirect php id Properly with .htaccess OpenLiteSpeed Server

0 comments
Message:


i am new to .htaccess usage and tried to learn through online resources but however i write it the rules negate each other and am having a hard time writing a good enough .htaccess file below is my current .htaccess file which works fine for some pages like removing extensions and rewriting subdomains please check below


## Flag for GoDaddy
Options +MultiViews

RewriteBase /

## Remove extensions
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !=f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

## Redirect from extensions to non-extensions
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,NE,L]

## Redirect Pages
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^post/([a-zA-Z0-9-/]+)$ /post.php?ps=$1
RewriteRule ^([a-zA-Z0-9-/]+)$ post-files.php?ps=$1 [L,QSA]

## Server Only

## Redirect from www - non-www
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://$1/$1 [R=301,L]


## SSL Redirect
## RewriteEngine On
## RewriteCond %{HTTPS} ≠On
## RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

## Create Error Pages
ErrorDocument 404 /errors/404.html
ErrorDocument 403 /errors/403.html
ErrorDocument 500 /errors/500.html

## Redirect non-existing pages to index.php
Options +SymLinksIfOwnerMatch
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

Above is the .htaccess am currently using and i got it through tutorials from youtube it works good and redirects files.php to files only which am happy with but
as you can see above this line RewriteRule ^post/([a-zA-Z0-9-/]+)$ /post.php?ps=$1 and the line below it are not passing through the ps but they show 404 page


i want the results to be domain.com/post-file-slug to go to exactly file domain.com/post-file.php?ps=post-slug-here for rule RewriteRule ^([a-zA-Z0-9-/]+)$ post-files.php?ps=$1 [L,QSA]


and


domain.com/post/post-slug-here to go to exactly domain.com/post.php?ps=post-slug-here for rule
RewriteRule ^post/([a-zA-Z0-9-/]+)$ /post.php?ps=$1


I was working on this for 2 days now hopefully fix it soon. Thanks



My Answer:
If you are trying to redirect PHP IDs properly with .htaccess on an OpenLiteSpeed server, you can use the following code in your .htaccess file:


RewriteEngine On
RewriteRule ^page/([0-9]+)$ page.php?id=$1 [L]


This code will redirect URLs like `example.com/page/123` to `example.com/page.php?id=123`. Make sure to replace `page.php` with the actual PHP file you want to redirect to, and adjust the regular expression `([0-9]+)` to match the pattern of your PHP IDs.

Additionally, make sure that your OpenLiteSpeed server is configured to allow the use of .htaccess files and mod_rewrite. You may need to enable the `RewriteEngine` directive in your server configuration.

If you continue to have issues with your .htaccess file, you may want to consult the OpenLiteSpeed documentation or seek assistance from a web developer or server administrator.

Rate this post

3 of 5 based on 8608 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us