RewriteEngine On
RewriteRule ^about/([^/.]+)/?$ about.php?id=$1 [L,QSA]
I have a page use htaccess rewrite rule for url
/about/33
the page require $_GET['id'] to fetch db
however i have trouble to detect GET variable isset
if(isset($_GET['id'])){fetch data...}else{header("location:...");}
if user only enter /about/ without $GET['id'] the page will not found instead run header("location");
is anyway to solve this?
try this:
RewriteRule ^about/([^/.]*)/?$ about.php?id=$1 [L,QSA]
Put * instead of +
Related
I have this type of url :
domain.com/articles/title
And i want to rewrite this url in :
domaic.com/articles.php?id=title
I use this rewrite condition:
RewriteEngine On
RewriteRule ^articles/(.*)$ articles.php?id=$1 [QSA,L]
I can show the page articles.php but i try to read value of id's variable with $_GET['id'] but is empty.
Thank in advance for your reply
I want that anybody who comes to my site
site.com?affiliate=a12b345c67
hides the affiliate querystring completely
I'm sure it's something like this, but nothing happens
RewriteRule ^/?affiliate= / [L,R=301]
To strip affiliate= query string from your URLs, place this rule in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^affiliate=[^&]+ [NC]
RewriteRule ^ %{REQUEST_URI}? [L,R=301,NE]
First of all you need to charge the URL from which users are coming to your website. Change it to:
site.com?/a12b345c67/
.htaccess cant change your URL structure on the fly.
Let me know if its ok.
Regards.
you could save the incoming $_GET parameter(if exists) to a session(or cookie, or whatever persistent storage) and reload the page.
session_start();
if(isset($_GET['affiliate']){
$_SESSION['affiliate'] = $_GET['affiliate'];
header('location: site.com');
}
you could then use the affiliate token from the session,cookie or whatever persistent storage
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]
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
On my site, users can add various URL's that need to be redirected.
For example; from this: domain.com/oldpage/36/
To this: domain.com/newpage/47/
They are added to the .htaccess like this:
Redirect 301 /oldpage/36/ /new-page/47/
But when accessing the old page they get this:
domain.com/newpage/47/?pid=36&pagename=oldpage
I'm pretty sure these rewrite rules are causing this predicament:
RewriteRule ([^.]+)/([0-9]+)/$ index.php?pid=$2&pagename=$1
RewriteRule ([^.]+)/([0-9]+)/([^.]+) index.php?pid=$2&pagename=$1&vars=$3
However, mod_rewrite stuff is not my strongpoint, so I have no idea how to fix it.
Any ideas ?
Adding a ? makes the Rewrite not add the query string to the url.
so this should work:
Redirect 301 /oldpage/36/ /new-page/47/?
As a precaution you could also add it to the end of:
RewriteRule ([^.]+)/([0-9]+)/$ index.php?pid=$2&pagename=$1?
RewriteRule ([^.]+)/([0-9]+)/([^.]+) index.php?pid=$2&pagename=$1&vars=$3?
But only if they are needed
Since you are already using mod_rewrite anyway, I suppose you should make your redirects using rewrites too
RewriteRule /oldpage/36/ /new-page/47/ [R=301]
This will "rewrite" the URL from old to new, and will redirect the browser to new url with status code 301. [R] directive means redirect, which also stops other rules from processing, hence the other rules will be handled only when the new request is sent from broswer with new url.