By using WordPress with Divi and custom php pages/scripts I ended with urls looking like this:
https://example.com/?day=today
https://example.com/?sport=1&league=123 (1=football and 123=premier_league)
I want to make them more user and SEO friendly and rewrite them in htaccess to:
https://example.com/?day=today ==> https://example.com/today
https://example.com/?sport=1&league=123 ==> https://example.com/football/premier_league
For the "day" link I tried this code, but it doesn't work
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9a-z\-\_]+)?$ /?day=$1 [L,QSA,NC]
</IfModule>
For the other example with sport and league I wanted to find a solution in which I will manually add old link and new link, tried redirect 301, but I think it can't work, right?
Redirect 301 https://example.com/?sport=1&league=123 https://example.com/football/premier_league
I also tried Redirection plugin in WP, but it created Rule which destroys page and makes error 500:
RewriteRule ?sport=1&liga=123 https://example.com/football/premier_league [R=301,L]
or
RewriteCond %{QUERY_STRING} ^sport=1&league=123$
RewriteRule ^$ https://example.com/football/premier_league [R=301,L]
which gaved address: https://example.com/premier_league/?sport=1&league=123
I was also wondering if I can make it with pure php, since in PHP I can easilly get proper strings for id's of the sports and leagues. Unfortunantelly changing "?sport=1&league=32758" into "?sport=football&league=premier_league" and then clean it with rewrite in htacces is not possible right now.
I'm not expecting whole code for both problems, I believe that some directions would be enough. I browse stack and google and coudn't find any examples with links that looks like mine, so example.com/?sport=1 not example.com/page?sport=1
After further investigation I believe the problem is in the way I'm opening my custom pages. I'm using shortcodes to initiate a script and inside the script my php code is included with parameters. I think that because of that I got to this page https://example.com/premier_league/?sport=1&league=123 which was pretty close, but not working.
Now my question is how to send those parameters to proper script but not into url.
Related
I'm creating a static webpage solution for my angularJS project where I check for the HTTP_USER_AGENT in my .htaccess file and then redirect any crawlers to a static php page so I can add all the necessary meta data.
The problem is that I'm using a wildcard subdomain, so any of my clients can have their own subdomain and I can't figure out how to redirect via RewriteRule to the right url.
My RewriteCondlooks like this:
RewriteCond %{HTTP_USER_AGENT} (facebookexternalhit/[0-9]|Facebot|Twitterbot|Pinterest|Google.*snippet)
and my basic non-working RewriteRule looks like this:
RewriteRule ^stuff/(.*)$ http://example.com/static.php?token=$1 [NC,L]
But I want it to go to static.php on that subdomain the request is on, so if I request http://subdomain.example.com/stuff?token=my-token is it possible to make the rewriterule go to http://subdomain.example.com/static.php?token=$1 instead of just the static mydomain.com above?
(there will NEVER be a request on http://example.com/stuff - all requests to stuff will be via a subdomain)
I'm not that experienced in .htaccess and get's a bit confused from all the answer that here listed at SO and Google finds.
I solved the issue by adding together pieces of code and a little bit of thinking.
This is the three lines in my .htaccess which does the trick for me:
RewriteCond %{HTTP_USER_AGENT} (facebookexternalhit/[0-9]|Facebot|Twitterbot|Pinterest|Google.*snippet)
RewriteCond %{HTTP_HOST} ^(.+?)\.example\.com$
RewriteRule ^s/(.*)$ http://%1.sociuu.com/static.php?token=$1 [NC,L]
If this can be done more elegant or smarter, please let me know otherwise I'll consider this as suitable for my needs :-)
Bounty Note: I've found my solution for my question using add_rewrite_rules. I will reward the bounty to anyone who can provide me with the same solution in apache RewriteRules format.
I've read How can I create friendly URLs with .htaccess? but it still difficult and complicate to me.
I am running WordPress Multisite, and I have a sub domain website cp.example.com and I'm writing a php app on this sub domain.
I have a website address that is looks like this:
http://cp.example.com/sales.php?id=12345&userid=123456&uid=83hkuhdqhuhd873xsuhdqwiuhdiq
Is it possible for me to let user access the website via:
http://cp.example.com/sales/12345/userid/123456/83hkuhdqhuhd873xsuhdqwiuhdiq
And if I were to do that will php still be able to do $_GET on the values?
e.g $_GET['id'], $_GET['userid'] and $_GET['uid']?
I'm using WordPress for my base, but i'm writing the app end, using a different table.
I'm trying to avoid using custom post type just to achieve the above.
I've tried the following.
Create a template file view_sales.php in view_sales.php I will require $_GET['id'], $_GET['userid'] and $_GET['uid'] in order to retrieve info from mysql.
in view_sales.php I've also used a different header file and have get_header('sales');
in header-sales.php I've added the following code gotten from the above stack overflow page.
$path_components = explode('/', $_GET['url']);
$id=$path_components[0];
$userid=$path_components[1];
$uid=$path_components[2];
I created a new wp page with slug sales so now the website is
http://cp.example.com/sales/?id=123456&userid=123456&token=98917397219372iheu1i
I only have one .htacess website in my /public_html/example.com domain since it's a multisite so I added the code suggested above to my .htaccess and now it looks like this.
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
# add a trailing slash to /wp-admin
RewriteRule ^([_0-9a-zA-Z-]+/)?wp-admin$ $1wp-admin/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?(wp-(content|admin|includes).*) $2 [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?(.*\.php)$ $2 [L]
RewriteRule . index.php [L]
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>
Nothing is working at the moment, still redirecting to the ugly url.
Edited:
I've tried the following
RewriteCond %{HTTP_HOST} !^cp\.example\.com [NC]
RewriteRule ^listing/([a-z0-9]+)$ listing/?action=$1 [L,NC]
RewriteRule ^view/([a-z9-9]+)$ view/?id=$ [L,NC]
I tried different variants of all the above but nothing works.
Edited for add_rewrite_rule method which i tried
function pa_custom_rules() {
add_rewrite_rule('^/listing/([^/]+)/$', 'index.php?pagename=listing&action=$matches[1]', 'top');
}
add_action('init', 'pa_custom_rules');
Printed the rewrite array and I can see the new rule
[^/listing/([^/]+)/$] => index.php?pagename=listing&action=$matches[1]
Visiting index.php works but going to /listing/test/ fails. It returns 404 error.
Finally solved problem which bothered me for last few days.
It appears Rewrite rules for specific domains does not work.
If you wish to change
http://subdomain.example.com/listing?action=add(e.g. or DEL or VIEW)
of a Wordpress MULTISITE to
http://subdomain.example.com/add/ (e.g.or DEL or VIEW)
you have to not only add rewrite rules, but it's also compulsory to rewrite/add the tag to query_var.
You need to add the following to function or create a plugin to initiate it once.
function pa_custom_rules() {
add_rewrite_rule('listing/(.*)$', 'index.php?pagename=listing&action=$matches[1]', 'top');
add_rewrite_tag('%action%', '(.*)');
}
add_action('init', 'pa_custom_rules');
once done you will be able to access http://subdomain.example.com/listing/add
In your page (in this case, my page name is 'listing'), you will no longer get 404 error and you can get the value of "action" (in this case Add/Del/View) by using
http://subdomain.example.com/listing/**add**
$a = get_query_var('action');
echo $a; //prints **add**
Note: You cannot get the value of action using $_GET like how RewriteRule works, but this should be able to get most things done.
I can keep working on wordpress and get all my customise beautiful links and throw away my last 3 days of work exploring Laravel and Symfony3. Thank God.
I have a website that is like this
http://cp.example.com/sales.php?id=12345&userid=123456&uid=83hkuhdqhuhd873xsuhdqwiuhdiq
Is it possible for me to let user access the website via
http://cp.example.com/sales/12345/userid/123456/83hkuhdqhuhd873xsuhdqwiuhdiq
Yes, its possible, e.g.:
RewriteEngine On
RewriteRule ^sales/(\d+)/userid/(\d+)/([a-z0-8]+)$ sales.php?id=$1&userid=$2&uid=$3 [L,NC]
And if i were to do that.. will php still be able to do $_GET on the
values?
e.g $_GET['id'], $_GET['userid'] and $_GET['uid']
Yes, of course php will be able do that.
I'm on my begining of learning PHP and very first steps of .htaccess, most of my new web is about main category and few subcategories.
Here are links examples i had before working out .htaccess RewriteEngine:
example.com/index.php?cat=email
example.com/index.php?cat=about&show=some
with help of .htaccess RewriteEngine i've convered them to:
example.com/email/
example.com/about/some/
Here is part of .htaccess:
RewriteRule ^([A-Za-z0-9-]+)/$ index.php?cat=$1 [L]
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/$ index.php?cat=$1&show=$2 [L]
RewriteRule ^([A-Za-z0-9-]+)$ index.php?cat=$1 [L]
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)$ index.php?cat=$1&show=$2 [L]
Now problem is that most of content have inside links like: "example.com/index.php?cat=about&show=some" Changing them all is option, but anyway... is there anything else could be done? I heard of some .htaccess option that autoconverts links to format you need without changing them manualy, so all links in PHP pages will be the same, but once user gets page loaded, links will be like (example.com/about/some/) Is there anything like that, or is there any other option to leave original link without changing them all?
Cheers!
Links on your site are created with your PHP scripts, Apache with htaccess can't magically change all this links in a raw.
Now what you can do, is redirect old URL to the new ones using htaccess, with a 301 redirection.
For example, if someone click on example.com/index.php?cat=email, Apache will redirect (= the URL will be changed and there will be another HTTP request) to example.com/email/, and then rewrite this to index.php?cat=email
Add this to your htaccess :
RewriteCond %{REQUEST_FILENAME} index.php$
RewriteCond %{QUERY_STRING} cat=([a-zA-Z0-9-]+)
RewriteRule ^ http://example.com/%1/? [L,R=301]
Anyway I strongly recommend you to change the links directly in your code, because even if the solution I've just explained should works (not tested), it's not really healthy to use redirection when you can avoid them.
Long time reader first time poster.
I have read through what feels like 100's on similar posts trying to make it fit what i am trying to do.
Apologies if its been covered. I believe I have the correct code but it maybe conflicting with something else?
I have old URLs from an old ASP cart and want to 301 redirect to new magneto static pages.
There are only 5 of these URLS so don't need a bulk action.
example:
OLD URL: http://test.com.au/shopcontent.asp?type=FAQ%20and%20Terms
NEW URL: http://test.com.au/privacy-policy-cookie-restriction-mode
I know that from reading other posts you can not simply use:
Redirect 301 /shopcontent.asp?type=FAQ%20and%20Terms /privacy-policy-cookie-restriction-mode
So I have worked out that this should work:
RewriteCond %{QUERY_STRING} ^type=FAQ%20and%20Terms$ [NC]
RewriteRule ^shopcontent\.asp$ /privacy-policy-cookie-restriction-mode/? [NC,R=301,L]
I have also tried removing the %20 for the spaces like this:
RewriteCond %{QUERY_STRING} ^type=FAQ\ and\ Terms$ [NC]
RewriteRule ^shopcontent\.asp$ /privacy-policy-cookie-restriction-mode/? [NC,R=301,L]
When I try and access the old URL it just goes to the 404 page. I am using standard .htaccess file that came with Magento (I can post if that helps);
I am using a VPS which mod rewrite is enabled. I have other Redirect 301's for non dynamic URLS.
Are there any logs that I can look at?
Appreciate any help or advice.
Thanks Rudi
ps. I found this post which got me to where i am now
RewriteCond %{QUERY_STRING} in mod_rewrite (dynamic to static URL) not working
You cant use this rule:
RewriteEngine On
RewriteCond %{QUERY_STRING} "^type=FAQ\%20and\%20Terms$" [NC]
RewriteRule ^shopcontent\.asp$ /privacy-policy-cookie-restriction-mode/? [NC,R=301,L]
I have some old SWF that has fixed links inside, so I need to rewrite all the links to redirect to new site I build on Wordpress.
I actually need to redirect all links that have FID=<123> to /me_<123> (that's the permalink I created for all pages I need to redirect)
For example:
http://kavor.org.il/KavOr07/Templates/showpage.asp?DBID=1&LNGID=2&TMID=843&FID=1997
Needs to go to http://kavor.org.il/me_1997
I tried to put this in .htaccess:
^FID=([^/.]+)/?$ /me_$1 [R]
but it has no effect and also for some reason slows down the entire site loading.
I know that Wordpress has its own rewrites so maybe it's interefering.
I would be glad for some help with that I got pretty lost in all the mod_rewrite rules...
To match query string you will need a RewriteCond with %{QUERY_STRING}
Try this rule:
RewriteCond %{QUERY_STRING} (^|&)FID=([^&]+) [NC]
RewriteRule ^KavOr07/?$ /me_%1? [L,R=302,NC]