.htaccess Reroute file with hidden parameter to another file - apache

I have a problem which I'm trying to solve nor for hours.
So in my root director i have a folder called plugins with some files in it. The important ones are plugins.php and query.php.
When the url is /test/plugins I wan't to show the content of plugins.php. But when the URL is /test/plugins/param I wan't to keep this URL in the browser but want to show the content of query.php and use param with $_GET['param'] in it. Is this possible?
My latest and most promising .htaccess file looks like this:
RewriteBase /test/
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-s
RewriteRule ^(.*)$ query.php?param=$1 [QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)$ query.php [QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} -s
RewriteRule ^(.*)$ query.php [QSA,NC,L]

With your shown samples and attempts, please try following htaccess rules file.
Make sure to keep your htaccess rules file inside plugins folder and also make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteRule ^test/plugins/?$ plugins.php [QSA,NC,L]
RewriteRule ^test/plugins/param/?$ query.php [QSA,NC,L]

Related

Apache RewriteRule not working – page not found

I have no idea why it doesn't work, /spelling/30000 gives the Not Found page. Please help.
RewriteEngine On
RewriteBase /
RewriteRule ^/([0-9]+)$ /?mod=spelling&word=$1 [PT]
RewriteRule ^$ /?mod=spelling [PT]
With your shown samples please try following .htaccess rules file. Considering that index.php file is the one which is taking traffic in backend for internal rewrite rules. Please make sure to keep your .htaccess file along side with index.php file.
Also make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteBase /
RewriteRule ^/?$ index.php?mod=spelling [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/?$ index.php?mod=$1&word=$2 [QSA,L]

Use ReWriteEngine to redirect user profile page to a physical page

Good day,
I want to rewrite my user id URL to a physical page on my website, see URL structure below:
From:
http://localhost:8888/report.mark1/user.php?id=341
To:
http://localhost:8888/report.mark1/341
This is the .htaccess file code:
ReWriteEngine On
RewriteRule ^report.mark1([^/.]+)?$ /user.php?id=$1 [L]
I'm I missing something?
With your shown samples, please try following htaccess rules file. Make sure your .htaccess file is present alongside with report.mark1 folder and your user.php is present inside report.mark1 folder.
Make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteBase /report.mark1/
##External redirect change of URL rules here.
RewriteCond %{THE_REQUEST} \s/(report\.mark1)/user\.php\?id=(\d+)\s [NC]
RewriteRule ^ /%1/%2? [R=301,L]
##Internal rewrite to user.php rules here.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(?:[^/]*)(\d+)/?$ /report.mark1/user.php?id=$1 [QSA,L]

Query string to path in htaccess somewhat working?

Hello I have been stuck with htaccess for the past 3 days and can not seem to figure out why my htaccess file is not changing my url correctly.
So I am attempting to turn this query string url from www.example.com/test/user-profile.php?id=4 into www.example.com/test/user-profile/4
the /test/ in the url is not a file, its a folder in the main public_html directory.
Here is my htaccess file
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-l
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ /test/user-profile.php?id=$1 [L]
RewriteCond %{QUERY_STRING} ^id=([0-9]*)$
RewriteRule ^(.*)$ /test/user-profile/%1? [R=301,L]
As of right now it is going to the url www.example.com/test/user-profile/4 but it is not loading the entire page. Just beyond stuck, thank you for your time
With your shown samples, attempts; please try following htaccess rules file. Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteBase /
##RewriteBase /test/
##Extrenal redirect rule.
RewriteCond %{THE_REQUEST} \s/(test/user-profile)\.php\?id=(\d+)\s [NC]
RewriteRule ^ %1/%2? [R=301,L]
##Internal rewrite rule.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/(.*)/?$ $1/$2.php?id=$3 [QSA,L]
JS/CS rewrite/redirect:
You may need to use base tag to fix your js and other relative resources. If you are linking js files using a relative path then the file will obviously get a 404 because its looking for URL path. for example if the URL path is /file/ instead of file.html then your relative resources are loading from /file/ which is not a directory but rewritten html file. To fix this make your links absolute or use base tag. In the header of your webpage add this <base href="/"> so that your relative links can load from the correct location.

.htaccess to customize php url in folder

My php file is in this location:
https://example.com/store/store.php?id=1
and I want to rewrite it as:
https://example.com/store/store_name
I tried like below:
RewriteEngine On # Turn on the rewriting engine
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([A-Za-z0-9_-]+)/?$ store.php?id=$1 [NC,L] #Handle page requests
But it is not working for me.
With your shown samples, please try following htaccess Rules file. Make sure to place your htaccess Rules file in folder where store folder is present, place it along side with store folder NOT inside it.
Make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^store/(.*)/?$ store/store.php?id=$1 [QSA,NC,L]

Conditional URL rewrite

I need the URL example.com/app to show the content from example.com/app/dist
The following code does exactly this.
RewriteEngine On
RewriteRule ^$ /app/dist/ [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/dist/
RewriteRule ^(.*)$ /app/dist/$1
BUT I also need the URL example.com/staging to show the content from example.com/staging/dist
With the current setup, the url example.com/staging is showing the content from example.com/app/dist
The .htaccess while is located in the /app and /staging folder (but it has to be the same file)
In the htaccess file in your staging folder, change all instances of app to staging
Edit:
You can try using relative paths instead of hardcoding the app or staging, but Ive had issues in the past, especially if there are other rules in play.
RewriteEngine On
RewriteRule ^dist - [NC,L]
RewriteRule ^$ dist/ [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ dist/$1 [L]
The idea is to remove the leading / from rewrites so a relative path is used. So technically it wont matter whatever folder the htaccess file is in