I have no idea how to play with .htaccess file to change / hide variables from URL
I want to use .htaccess file of WordPress
my .htaccess file already contains this scripts:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /my_site/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /my_site/index.php
[L]
</IfModule>
# END WordPress
I have url
http://localhost/my_site/project-detail/?pro_id=7
and I want to either hide pro_id=7 like
http://localhost/my_site/project-detail/
but could be able to catch this $_GET['pro_id'] in my php script file.
or
http://localhost/my_site/project-detail/7
I also tried
RewriteRule ^project-detail/(.*) /project-detail/?pro_id=$1
Have you tried something like that:
RewriteEngine On
RewriteRule ^project-detail/([0-9]+)$ project-detail/?pro_id=$1
You could try adding these above the RewriteBase /my_site/ directive, as they need to be before your wordpress stuff.
First, you can access the actual request directly to redirect to the URLs without the query string:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /my_site/project-detail/?\?pro_id=([^&\ ]+)&?([^\ ]*)
RewriteRule ^ /my_site/project-detail/%1?%2 [R=301,L]
This will make it so a URL like this: http://localhost/my_site/project-detail/?pro_id=7 gets redirected to http://localhost/my_site/project-detail/7 and a URL like http://localhost/my_site/project-detail/?pro_id=7&other=param gets redirected to http://localhost/my_site/project-detail/7?other=param. If you don't want any extra params at all, remove the %2 from the rule's target. This makes it so the browser's address bar changes to the URL without the pro_id query string. Now we have to internally rewrite it back to the query string:
RewriteRule ^my_site/project-detail/([^/]+)$ /my_site/project-detail/?pro_id=$1 [QSA,L]
So internally, the URI gets rewritten back to /my_site/project-detail/?pro_id=7 but the browser's address bar still has the clean looking URL.
Related
I Have a website which has https://www.example.com/index.php?page=abc
I want this URL to be open like this https://www.example.com/teachers
I have tried it through .htaccess redirect code but it returns "Page not found".
Below is my .htaccess code
RewriteCond %{QUERY_STRING} (^|&)page=teachers($|&)
RewriteRule ^index\.php$ /teachers? [L,R=301]
For your teachers URL, you can use the following rule to convert your clean url to a .php file with a query string:
# URL
# INPUT https://www.example.com/teachers
# OUTPUT https://www.example.com/index.php?page=teachers
RewriteEngine On
RewriteRule ^(.*)$ /index.php?page=$1 [L]
View on htaccess.madewithlove.be
This allows user to visit https://www.example.com/teachers, and inside your index.php you can $_GET['page'] to receive the teachers string.
I have a url like this:
http://www.localhost.com/code_category/computers/
I want to change this url to:
http://www.localhost.com/category/computers/
I don't need url redirection.
My current htaccess file looks like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
You only want to redirect code_category to categoryexternally and keep the path as it is internally so, try this :
RewriteCond %{THE_REQUEST} !\s/+category/ [NC]
RewriteRule ^code_category/(.*)$ category/$1 [R=302,L,NE]
RewriteRule ^category/(.*)$ code_category/$1 [L]
The above will redirect any request containscode_category/whatever to category/whatever externally and keep the internal path as it is .
If you want only request contains code_category/computers/ change it to this :
RewriteCond %{THE_REQUEST} !\s/+category/computers/ [NC]
RewriteRule ^code_category/computers/(.*)$ category/computers/$1 [R=302,L,NE]
RewriteRule ^category/computers/(.*)$ code_category/computers/$1 [L]
test it , if it is fine change 302 to 301 for permanent redirection.
Note: clear your browser cache then test it.
.htaccess file
Add this code
RewriteEngine on
RewriteCond %{HTTP_HOST} ^localhost.com [NC,OR]
# without redirect
# RewriteRule ^/code_category/computers/$ category/computers/
RewriteRule ^/category/computers/$ code_category/computers/
# redirect method
# RedirectMatch 301 ^/code_category/computers/$ category/computers/
RewriteEngine On enables mod_rewrite.
RewriteCond %{HTTP_HOST} shows which URLs we do and don't want to run through the rewrite.
In this case, we want to match example.com.
! means "not." We don't want to rewrite a URL that already includes folder1, because then it would keep getting folder1 added, and it would become an infinitely long URL.
[NC] matches both upper- and lower-case versions of the URL.
RewriteRule defines a particular rule.
The first string of characters after RewriteRule defines what the original URL looks like. There's a more detailed explanation of the special characters at the end of this article.
The second string after RewriteRule defines the new URL. This is in relation to the document root (html) directory. / means the html directory itself, and subfolders can also be specified.
For Reference click here
Hope this helps!
I've taken over a former site/domain, and set up a new site using Wordpress. The WP installation rewrites URL's to static ones, as you'd expect it to.
At the same time I want to preserve the former pages, as they have incoming links. I'm not interested in 301'ing them to "new" pages.
The old URL structure is /index.php?id=123, which I suspect is causing the problem with the WP .htaccess file. For reference, this is what it looks like:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
I've tried adding the following:
RewriteRule ^([0-9]+).html index.php?id=$1 [R,L]
Doesn't work. Just redirects to site.com/?id=123 and shows the front page.
I should add that I plan on just adding these new pages as regular static HTML files in the format of 123.html, 321.html etc.
How do I use .htaccess to make this work together with the WP installation and what WP puts into the .htaccess file?
To clarify:
I want to have my 123.html static HTML page be index.php?id=123. When you access index.php?id=123 it should bring up 123.html, but show index.php?id=123 in the address bar. If you access 123.html it should 301 to index.php?id=123.
To map an URL with a querystring up to an actual file you'll need to use a RewriteCond to match the querystring itself (as RewriteRule doesn't):
Something along these lines ought to do it:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# retrieve X.html when index.php?id=X is requested
RewriteCond %{REQUEST_FILENAME} index\.php
RewriteCond %{QUERY_STRING} ^id=([0-9]+)$
RewriteCond %1.html -F
RewriteRule .* %1.html? [L]
# standard WordPress routing
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
This will first check to see if you've got a request for index.php with a querystring like id=X.
Then it'll check to see if a file called X.html actually exists; I'm not 100% happy about having to use the more system hungry subrequest file check -F rather than the standard -f but I can't see a way around it in .htaccess in this case.
If X.html actually exists, it'll fetch that file whilst leaving the URL as index.php?id=X.
However if that file doesn't exist it'll fall back to standard WordPress no file, no directory routing to index.php
I'm not a WordPress expert but that should work; I guess the main WordPress controller uses $_SERVER['REQUEST_URI'] to determine the action.
Note: This won't, however, prevent people from accessing 123.html directly by going to the URL www.site.com/123.html - I kept falling into infinite loops and Apache 500 errors trying to prevent that :|
here is my website
http://www.coolcodez.net/ios/nicucalc
notice when you click on pages on the nav you get urls like
http://www.coolcodez.net/ios/nicucalc/index.php?page=features
I put an .htaccess file in my nicucalc directory. I want the urls to look like this
http://www.coolcodez.net/ios/nicucalc/features
even better would be
http://www.coolcodez.net/nicucalc/features
here is my htaccess file. It's never working properly..
RewriteEngine on
RewriteCond %{QUERY_STRING} ^page=(.*)$
RewriteRule ^index\.php$ /%1/? [R=301,L]
what am i doing wrong. explanation as well please
also note: this folder is located inside of a wordpress installation folder. not sure if that htaccess file would be affecting mine somehow
The rule that you have redirects requests for index.php to /features/ (or whatever the "page" is). This is fine in and of itself but you need something that rewrites it internally back to index.php. Because of that you need 2 rules, one to redirect (matches request) and one to internally rewrite (matches URI):
RewriteEngine On
RewriteBase /ios/nicucalc/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /ios/nicucalc/index\.php\?page=([^&\ ]+)&?([^\ ]*)
RewriteRule ^ /ios/nicucalc/%1?%2 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?page=$1 [L,QSA]
The first rule matches against the request, this looks like:
GET /ios/nicucalc/index.php?page=features HTTP/1.1
The "features" is captured and backreferenced in the rule using %1. The second rule first checks if the request points to an existing file or directory. If it doesn't then the URI is captured and then rewritten to index.php and the URI gets passed to the script via the "page" parameter.
I want to catch every request that comes in on my domain name and redirect that url to a php file. In this PHP file I show a form and when the user passes the form I want to redirect the user to the page it requested orignally.
I've got the following mod_rewrite:
RewriteEngine on
RewriteCond %{HTTP_COOKIE} !(accepts_cookie) [NC]
RewriteRule ^(.*) test.php?link=.. [L]
As you see I would like to place the full requested url in the link= but how am I able to do this? I tried stuff like: REQUEST_URI, but that doesn't give me the full path including query strings.
I hope someone can help!
Try this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
Options +FollowSymLinks
RewriteCond %{HTTP_COOKIE} !(accepts_cookie) [NC]
RewriteRule ^(.*)$ test.php?link=%{REQUEST_URI}%{QUERY_STRING} [L]
</IfModule>
You can always consider the mod_rewrite Cheat Sheet.