I am using a GET variable to select a specific item to display on the page. My URL would usually look like:
/?type=xxxx
I then have a login plugin (Wordpress) which adds ?redirect=ok to the URL on submission. Unfortunately, this then makes the URL look like: /?type=xxxx?redirect=ok which breaks the previous variable.
I am trying to edit that URL somehow. I thought about a few methods:
.htaccess to find and rewrite the ?redirect=ok to blank or to &redirect=ok
PHP to strip that part out of the string and then redirect
None of these seemed to work.
Any help would be massively appreciated.
Here is mod_rewrite fix for this strange problem.
Put this code in your DOCUMENT_ROOT/.htaccess file as very first rule:
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/+([^?]*\?type=.*?)\?(redirect=ok) [NC]
RewriteRule ^ %1&%2 [R=301,L]
Related
I know there's a million similar questions on stuff like this, but clearly there's much I don't understand because I haven't been able to derive answers or a solution to my (as I understand it) fairly simple question.
Basically, I'm trying to get an old site back up, but want a more professional look to it this time round, which includes cleaning up the URLs. A typical page is as follows (hosted locally at the moment, but will be assigned a domain in next few days):
192.168.0.200/album-reviews.php?albid=22
Using the following code, I have been able to achieve the above example page loading via manually typing 192.168.0.200/album-reviews/22 into the browser:
RewriteRule ^album-reviews/([0-9a-zA-Z]+) album-reviews.php?albid=$1 [NC,L]
However, what I want as well is for when the link is clicked on my site, it directs the user to /album-reviews/22 instead of album-reviews.php?albid=22. The only way to get the clean URL at the moment is to manually type it into the bar, links from my site do not get the clean URL, the code I have been playing around with (and have been unable to get working) based on sources I've found is this:
RewriteCond %{THE_REQUEST} /album-reviews/?(?:\.php)?\?albid=([0-9a-zA-Z]+)
RewriteRule ^ /album-reviews/%1? [L,R]
So if anyone could shed some light on how I get all this working as desired, I'd be grateful, I hope my question has been articulated appropriately.
On a side note, If i wanted to include the post title in the URL too like this:
192.168.0.200/album-reviews.php?albid=22&ptitle=my first post
how would alter any code to make it like this:
192.168.0.200/album-reviews/22/my first post
Thank you.
You can use:
Options -MultiViews
RewriteEngine on
RewriteCond %{THE_REQUEST} /album-reviews/?(?:\.php)?\?albid=([^\s&]+)&ptitle=([^\s&]+)
RewriteRule ^ album-reviews/%1/%2? [L,R=301]
RewriteCond %{THE_REQUEST} /album-reviews/?(?:\.php)?\?albid=([^\s&]+)
RewriteRule ^ album-reviews/%1? [L,R=301]
RewriteRule ^album-reviews/([^/]+)/?$ album-reviews.php?albid=$1 [NC,L]
RewriteRule ^album-reviews/([^/]+)/([^/]+)/?$ album-reviews.php?albid=$1&ptitle=$2 [NC,L]
If you want to show the contents of http://yoursite.com/album-reviews.php?albid=22 at the url http://yoursite.com/album-reviews/22, you need these codes:
Your htaccess needs this lines:
RewriteEngine On
Options -Indexes
RewriteRule ^album-reviews/(.*)$ album-reviews.php?pretty=$1 [L,QSA]
And your PHP these:
$pretty = $_GET['pretty'];
$parameters = explode('/',$pretty);
$albid = $parameters[0];
Your user won't be redirected, your website will show directly the page at the pretty url.
Now, what happened? That you instructed htaccess to send everything after album-reviews as a GET parameter called "pretty". Then in your PHP you cut it for every / that appeared in it, and that way you formed the array $parameters. So you can even get more parameters, for every /, they are all in the array $parameters:
$second_parameter = $parameters[1];
$third_parameter = $parameters[2];
$fourth_parameter = $parameters[3];
I have one problem, Once page in WordPress need a parameter in $_GET.
I got URL,
example: homepage.com/something/?param1=someString,
I would like to get homepage.com/something/someString <--- someString would like to get in table $_GET. I tried by htaccess but it's not working.
RewriteCond %{REQUEST_URI} ^something/ [NC]
RewriteRule ^/something/([a-z]+)/?$ /something/?param1=$1 [L]
Additional when I tried homepage.com/something/someInteger It's going to the correct page, "something" but when I tried homepage.com/something/someString <---
It's going to 404 page, why?
Somebody any ideas?
I noticed on youtube their url does not have a file extension and querystring. I've been trying to emulate something similar but found I had to either include the file extension or a trailing slash.
members.php?agefrom=20&ageto=40&city=london (works)
members/?agefrom=20&ageto=40&city=london (works)
members?agefrom=20&ageto=40&city=london (doesnt work)
So I was just wondering how can I get the third case to work? i've tried a few things in the htaccess file.
RewriteRule ^members$ index.php?page=members&%{QUERY_STRING} [QSA,L]
I have tried the above but to no avail.
Any help would be appreciated.
The RewriteRule that you posted is correct for members.php? and for members? It should not work with members/
You must have additional RewriteRules before this one that are getting applied first and are affecting this rule.
However, here is a rule that should still work for you:
RewriteRule ^members/?$ index.php?page=members&%{QUERY_STRING} [QSA,L]
The /? is saying to match if the slash exists or if it doesn't exist.
Have you tried to remove the $ on the end?
RewriteRule ^members/?$ index.php?page=members&%{QUERY_STRING} [QSA,L]
This did work in the end, all I had to do was move it nearer the top of the htaccess file. I had the following line which I guess was being read instead.
....
RewriteCond %{REQUEST_URI} ^/members$ [OR]
....
I am changing my approach to SEO URL's because I was trying to find articles on how the googlebot actually crawls forms and how it prefers the GET method. I was using jquery to alter my action parameter to write the following URL:
/members/london/18-to-25
I dont know how much google likes jquery and whether it would scan javascript code. I am assuimg it just follows the HTML code and having done some research I have changed my form to use the GET method and so the bot can crawl my form without complaining so now my URL looks like this:
/members?location=london&agefrom=18&ageto=40
I am on the right track to assume this? or should I just stick with jquery to rewrite the action parameter for an seo friendly URL?
I'am redirecting about 100 hmtl pages to a single PHP page (example.php) using .htaccess. It is working perfectly.
I've pagination on that page (example.php) but I am using the original HTML page URL (example.html?page=2&limit=20)
so example.html, example1.html, example2.html, example3.html are all redirecting to example.php.
The address bar is still showing ".html" URL but due to .htaccess redirection the example.php is rendering.
when is click on a pagination link (example.html?page=2&limit=20) the browser address bar shows correct .html URL and query string.
I've tried to get the values of page, and limit using $_GET and $_REQUEST in (example.php) but i am not successful.
Please help me in reading the (example.html?page=2&limit=20) query string parameeters .
Edit Code ported from comments:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !^page-(.*)$
RewriteRule ^page-(.*)$ size-content.php?sef=$1 [L]
Add the QSA flag, which means "query-string append" to be sure the existing query string is ported into the rewritten URL.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !^page-(.*)$
RewriteRule ^page-(.*)$ size-content.php?sef=$1 [L,QSA]
.htaccess modify your server configuration.
if you are making redirection then you change your request.
Try mod_rewrite if you are using Apache of course.
RewriteEngine On
RewriteRule ^example\.html\?(.*) example.php?$1
Mod rewrite is module to Apache. It is not allowed on most free hostings.
Yasir - you can resolve this problem by two ways:
1) Re-write rules for .htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !^page-(.).html(.)/(.)$
RewriteRule ^page-(.).html(.)/(.)$ size-content.php?sef=$1&page=$2&limit=$3 [L]
This rule will handle: page-example.html?page=2&limit=20
I hope - you will easily understand the above rule.
Note: Keep one thing in your mind that every link should be in same pattern if you change rule in htaccess.
2) You can resolve this problem on your "size-content.php"
Suppose page-example.html?page=2&limit=20
$_GET['sef'] = example.html?page=2&limit=20 [according to you .htaccess]
Now you can parse this string via explode function
Thanks
I am pretty new to using the RewriteRule, so I am likely missing something obvious, but I have a PHP script that takes URL variables like this:
{baseurl}properties.php?prop=Property-Name
I would like to create RewriteRules so that anyone who types in this script name/variable combo would have their URL rewritten to:
{baseurl}/properties/Property-Name
As well as ensuring that anyone who types in the flat-link url, actually calls the script with the right variable name and value.
I have been referring to this link and I have found related threads:
Mod_rewrite flat links
Mod_rewrite trouble: Want to direct from ?= to a flat link, nothing seems to work
But, I am obviously doing something wrong, as I cannot get this URL to work the way I want. I am currently using the following code, which appears to do nothing (aside from rewriting the URL to include the www, and redirect requests for index.php to the site root):
RewriteEngine ON
RewriteCond %{HTTP_HOST} ^baseurl.com$ [NC]
RewriteRule ^(.*)$ http://www.baseurl.com/$1 [R=301,L]
RewriteRule ^index.php / [R=301,L]
RewriteRule ^properties/([0-9A-Za-z]+)/$ /properties.php?prop=$1
The issue is clearly with the last RewriteRule, assuming nothing above is affecting it. Again, I am likely doing something ridiculous. Can someone please explain what I am doing wrong?
Thanks for your help.
At a quick glance, it appears that you forgot to include the dash in your regular expression and you included trailing slash. Use this instead:
RewriteRule ^properties/([0-9A-Za-z-]+)$ /properties.php?prop=$1
If you look at your rule ^properties/([0-9A-Za-z]+)/$ you see that it needs to end with a forward slash. You can either remove that or make it optional like ^properties/([0-9A-Za-z]+)/?$.