This question already has answers here:
htaccess rewrite for query string
(4 answers)
Closed 3 years ago.
How can I hide $_GET parameters from a URL?
I'm trying to get a URL like this: mysite.com/profile/?id=76561190000000009
To be usable like this: mysite.com/profile/76561190000000009/
My website already has a rewrite rule for hiding file types (.php, .html, etc.) and adding a trailing slash to the end of URL's so I haven't had any success with solutions I've found on here/around the web.
Current .htaccess I'm using:
RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
Try this:
RewriteEngine On
RewriteRule ^profile/([^/]*)/$ /profile/?id=$1 [L]
This probably is what you are looking for:
RewriteEngine On
RewriteRule ^/?profile/(\d+)/?$ /profile/?id=$1 [END]
In case you receive an internal server error (http status 500) using the rule above then chances are that you operate a very old version of the apache http server. You will see a definite hint to an unsupported [END] flag in your http servers error log file in that case. You can either try to upgrade or use the older [L] flag, it probably will work the same in this situation, though that depends a bit on your setup.
This implementation will work likewise in the http servers host configuration or inside a dynamic configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a dynamic configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those dynamic configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).
Solved my problem by using this rewrite rule:
RewriteEngine On
RewriteRule ^(.*)$ index.php?id=$1 [L,QSA]
From this answer.
Related
I can't figure out how to cut extension (.php) in .htaccess in case I already have 1 virtual sub directory.
1) localhost/admin/index.php?lang=en -> localhost/admin/en/
2) localhost/admin/index.php?lang=ru -> localhost/admin/ru/
For case 1) and 2) I wrote already and it works.
Resolved with -> RewriteRule ^([a-z]+)(/)?$ index.php?lang=$1 [QSA,L]
Please consider that (en & ru are virtual directories)
3) localhost/admin/index.php?lang=en/createprofiles.php -> localhost/admin/en/createprofiles.php
4) localhost/admin/index.php?lang=ru/createprofiles.php -> localhost/admin/ru/createprofiles.php
For case 3) and 4) I wrote also and it works.
RewriteRule ^([en|ru]{2})/(.*)$ $2?lang=$1&%{QUERY_STRING} [L,QSA]
But I would like to cut .php extension.
The final link must be like : localhost/admin/en/createprofiles/
Can you please help me :). I have tried everything (about 8 hours), but nothing works
Thank you !
This would be an example which implements both directions:
requests to the "pretty URLs" are internally rewritten to the actual scripts and
(possible) requests to the actual scripts are externally redirected to the "pretty URLs"
This obviously is just an example, your real world scenario is typically a bit more complex. But this example should point you into the right direction...
RewriteEngine on
RewriteCond %{QUERY_STRING} (?:^|&)lang=(en|ru)(?:&|$)
RewriteRule ^/?admin/?$ /admin/%1 [QSA,R=301]
RewriteCond %{QUERY_STRING} (?:^|&)lang=(en|ru)(?:&|$)
RewriteRule ^/?admin/index\.php$ /admin/%1 [QSA,R=301]
RewriteCond %{QUERY_STRING} (?:^|&)lang=(en|ru)(?:&|$)
RewriteRule ^/?admin/createprofiles\.php$ /admin/%1/createprofiles [QSA,R=301]
RewriteRule ^/?admin/(en|ru)/?$ /admin/index.php?lang=$1 [QSA,END]
RewriteRule ^/?admin/(en|ru)/createprofiles/?$ /admin/createprofiles.php?lang=$1 [QSA,END]
It is a good idea to start out with a 302 temporary redirection (instead of the R=301) and only change that to a 301 permanent redirection later, once you are certain everything is correctly set up. That prevents caching issues while trying things out...
In case you receive an internal server error (http status 500) using the rule above then chances are that you operate a very old version of the apache http server. You will see a definite hint to an unsupported [END] flag in your http servers error log file in that case. You can either try to upgrade or use the older [L] flag, it probably will work the same in this situation, though that depends a bit on your setup.
This implementation will work likewise in the http servers host configuration or inside a dynamic configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a dynamic configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those dynamic configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).
This question already has answers here:
URL rewriting with PHP
(5 answers)
Closed 3 years ago.
I would appreciate if someone could help me. I’m getting so confused.
I want to redirect
https://xklsv.me/viewblog.php?title=working-plans-survey-and-demarcation-of-the-karnataka-forest-department/aranya_kfd/November-8th-2018
To:
https://xklsv.me/working-plans-survey-and-demarcation-of-the-karnataka-forest-department/aranya_kfd/November-8th-2018
but 301 moved permanantly not working giving 200 ok status code
Well, this sounds pretty straight forward, exactly as the documentation shows in examples:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^title=working-plans-survey-and-demarcation-of-the-karnataka-forest-department/aranya_kfd/November-8th-2018$
RewriteRule ^/?viewblog\.php$ /working-plans-survey-and-demarcation-of-the-karnataka-forest-department/aranya_kfd/November-8th-2018 [R=301,QSD]
RewriteRule ^/?working-plans-survey-and-demarcation-of-the-karnataka-forest-department/aranya_kfd/November-8th-2018/?$ /viewblog.php?title=working-plans-survey-and-demarcation-of-the-karnataka-forest-department/aranya_kfd/November-8th-2018 [END]
If that should be implemented in a more general way that should work:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^title=([^&]+)$
RewriteRule ^/?viewblog\.php$ /%1 [R=301,QSD]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?([^/]+)/?$ /viewblog.php?title=$1 [END,QSA]
You may need to tweak this a little, but it should point you into the right direction...
It is a good idea to start out with a 302 temporary redirection and only change that to a 301 permanent redirection later, once you are certain everything is correctly set up. That prevents caching issues while trying things out...
In case you receive an internal server error (http status 500) using the rule above then chances are that you operate a very old version of the apache http server. You will see a definite hint to an unsupported [END] flag in your http servers error log file in that case. You can either try to upgrade or use the older [L] flag, it probably will work the same in this situation, though that depends a bit on your setup.
This implementation will work likewise in the http servers host configuration or inside a dynamic configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a dynamic configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those dynamic configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).
On my website I want to internally execute a url like mydomain.com/dir/test as mydomain.com/dir/process.php?arg=test
I already created a htaccess file within the folder "dir", and when I try to access mydomain.com/dir/test, it displays the page of process.php, but when I try to access $_GET["arg"], it does not contain the string test but the string process.php.
My .htaccess:
RewriteEngine On
RewriteRule ^(.*)$ process.php?arg=$1 [L]
Reason for your result is a rewriting loop. You try to stop the rewriting process by using the [L] flag, but as many others you miss understand what that flag actually does. It does not end the rewriting process, but only the current run of that process. In your case the rewriting process starts another run and again rewrites your already rewritten request. In the second run the pattern ^(.*)$ matches what you have rewritten to in the first run, that explains your outcome.
You have two options to solve this.
1) use a condition to prevent the second rewriting run:
RewriteEngine On
RewriteCond %{REQEUEST_URI} !/process\.php$
RewriteRule ^(.*)$ process.php?arg=$1 [L]
2) use the END flag which is provided by newer versions of the apache http server:
RewriteEngine On
RewriteRule ^(.*)$ process.php?arg=$1 [END]
In case you receive an internal server error (http status 500) using that second rule above then chances are that you operate a very old version of the apache http server. You will see a definite hint to an unsupported [END] flag in your http servers error log file in that case.
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those dynamic configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).
I want to make a url like this
if the parameter is only one
it will be localhost/site/news from index.php?page=news
this is the .htaccess
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?page=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ index.php?page=$1
but if the link like this index.php?page=news&halaman=2
i want to make it like this localhost/site/news/2
i have try this but its not working
RewriteRule ^(.+?)/(.+?) index.php?p=$1&halaman=$2 [NC,L]
how can i resolve this? Thanks...
Your attempt points into the right direction, I only took the liberty to clean it up a bit:
RewriteEngine on
RewriteCond %{REQUEST_URI} !/index\.php$
RewriteRule ^/?(\w+)/?$ /index.php?page=$1 [END,QSD]
RewriteCond %{REQUEST_URI} !/index\.php$
RewriteRule ^/?(\w+)/(\d+)/?$ /index.php?page=$1&halaman=$2 [END,QSD]
In case you receive an internal server error (http status 500) using the rule above then chances are that you operate a very old version of the apache http server. You will see a definite hint to an unsupported [END] flag in your http servers error log file in that case. You can either try to upgrade or use the older [L] flag, it probably will work the same in this situation, though that depends a bit on your setup.
This rule will work likewise in the http servers host configuration or inside a dynamic configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a dynamic configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those dynamic configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).
I'm a beginner in PHP, there is a one thing which I haven't been able to find an answer or I just don't know what to search.
I've made a PHP script which shows user info from database using GET parameter.
The url looks like this http://127.0.0.1/user-info.php?userid=testuser what I want to do is to make the url look like this http://127.0.0.1/user-info/testuser. How is this possible, is it done with .htaccess or using php and how?
Thank you in advance.
Sure this is possible, rewriting is usually done on the protocol level, so inside the http server, it has nothing to do with the php engine:
RewriteEngine on
RewriteRule ^/?user-info/(.+)$ /user-info.php?userid=$1 [END]
This rule works likewise in the http server's host configuration or in a dynamic configuration file (".htaccess" style file). For this to work the http server's rewriting module has to be enabled, obviously. And if you decide to use a dynamic configuration script that also will have to be supported and enabled.
In case you receive an "internal server error" (http status 500) using above rule then chances are that you operate a very old version of the apache http server. In that case replace the END flag with the L flag, should work too in this case, though it depends on other rewriting rules you have. In any case you will find a definite hint on the unknown END flag in the http servers error log file.
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those dynamic configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).
I think you need something like this, give it a try. Just replace url(index.php with user-info.php)
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^([a-zA-Z]*)/?([a-zA-Z]*)?/?([a-zA-Z0-9]*)?/?$ index.php?controller=$1&action=$2&id=$3 [NC,L]
OR
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
You can get a .htaccess file from zend expressive, it has everything you need - https://github.com/zendframework/zend-expressive-skeleton/blob/master/public/.htaccess. It passes all requests to index.php file where you can do further routing and then request processing.
As a router I recommend FastRoute (https://github.com/nikic/FastRoute). Also take a look at composer - it is a must have tool for php developer.