URL structure change using .htaccess rule - php

I have no knowledge in regex and never done regex redirections.
htaccess
but... I am looking for a way to redirect this URL structure:
http://www.example.com/mypage/param1/
to this URL structure
http://www.example.com/mypage/?key1=param1
and, if possible, only if param1 contains # or %40
I saw this answer:
How to change URL structure with htaccess?
but that's the reveresed direction for my question

RewriteEngine on
RewriteCond $1 #
RewriteRule ^mypage/((?!index).+)$ /mypage/?key=$1 [NC,L,R]
Remove the R flag if you dont want the url to change.
This will redirect an url of the form :
/mypage/.*#.*
to
/mypage/?key=.*#.*

Related

htaccess rewrite rule for url varible as parent folder

I already create a .htaccess in my folder and would like to make the URL:
www.example/good/page
toward:
www.example/page?type=good
my current document is written as:
RewriteRule ^(\w+)$\/page page.php?type=$1 [NC,L]
but it didn't work, and I don't know how to check it is correct or not
would anyone able to provide some example for that?
thanks!
Make sure your rewite engine is on, and it is rewriting the base. Change your code like the following:
RewriteEngine on
RewriteBase /
RewriteRule ^/?([^/]+)/page?$ "page.php?type=$1" [L,QSA]
And then your rewrite rule must look like this.
The RewriteRule basically means that if the request is done that matches ^/?([^/]+)/page?$ (matches any URL except the server root), it will be rewritten as page.php?type=$1 which means a request for page.php be rewritten as page.php?type=good).
QSA means that if there's a query string passed with the original URL, it will be appended to the rewrite (example.com/good/page?id=2 will be rewritten as page.php?type=good&id=2.
L means if the rule matches, don't process any more RewriteRules below this one.
Try this and let me know. Accept the answer if it worked for you.

Extract URL slug using PHP. Building a URL shortener

I'm building a URL shortening web app using PHP. I am able to generate shorter URLs successfully. But I'm not able to redirect the users when they visit the shortened URL.
If the user enters https://example.com/aBc1X, I'd like to capture the aBc1X. I'll then query the database to find the original URL and then redirect.
My question is, how can I extract the aBc1X from the above URL?
P.S. I'll use either Apache or Nginx.
Two things to do for you.
First you have to redirect all traffic to one file which will be your router file. You can do this by placing a few rules in .htaccess file. I will put there some generic rules to start with (this one come from Wordpress):
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^redirect\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /redirect.php [L]
</IfModule>
They tell that everywhere url points to which isn't file or directory will run file redirect.php. You may want to tweak that settings to your needs.
Then in redirect.php you can capture url by looking inside $_SERVER['REQUEST_URI'].
For url http://example.com/any-url-i-want you would have
$_SERVER['REQUEST_URI'] == '/any-url-i-want.
Now the only thing you need is to find the url in database, and do a redirect.
I guess you can handle string operations at this point, either by using parse_url, regular expressions, or simple string cutting.
You want to use;
$_SERVER['REQUEST_URI'];
That will return what you're looking for.
You can see the documentation here
To parse the url
$url = "https://example.com/aBc1X";
$path = ltrim(parse_url($url, PHP_URL_PATH), '/');
Then $path will be aBc1X as desired. Note that any query following a ? will be omitted in this solution. For more details have a look at the documentation of the parse_url function.

htaccess - Redirect to the same location irrespective of presence of trailing slash

I want to redirect the following sets of links:
a/b/c or a/b/c/ to a.php?b=c
x/y1/z1/y2/z2 or x/y1/z1/y2/z2/ to x.php?y1=z1&y2=z2
using htaccess and mod rewrite in a standardized general format associating the appropriate PHP get tags and values to the corresponding SEO-friendly link. How do I do so?
I've tried tinkering around with RewriteCond and REQUEST_FILENAME but just cannot seem to get it to work.
Something like this might help:
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/? /$1.php?$2=$3 [NC,L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/? /$1.php?$2=$3&$4=$5

How can I forward a dynamic URL to twitter using .htaccess or PHP?

I need to rewrite a dynamic URL. The content myurl and mytext is always different and should be insert in the "text" and "url" from the Twitter string.
This is the string:
http://example.com/share/?myurl=http%3A%2F%2Fwww.example.com&mytext=helloworld
/* forward to: */
http://twitter.com/intent/tweet?related=Example%3Aname&text=helloworld&url=http%3A%2F%2Fwww.example.com&via=example
How can that be made? (I browsed the .htaccess suggestions but couldn't find a solution for my specific problem.)
Does your server support PHP?
You could just put something like the following in a PHP file:
Header("Location: http://twitter.com/intent/tweet?related=Example%3Aname&text=$_GET['mytext']&url=$_GET['myurl']&via=example");
It will be more efficient to handle it with rewrite Rules.
RewriteEngine On
RewriteCond %{QUERY_STRING} ^myurl=(.*)&mytext=(.*)$
RewriteRule ^(.*)$ http://twitter.com/intent/tweet?related=Example%3Aname&text=%2&url=%1&via=example [R,L]
If you want this to be permanent redirect then replace R with R=301.
With permanent redirects , these links will always be redirected by browser and your server will have to deal with less traffic if that is desired.

remove http from URL in .htaccess (mod_rewrite)

Been stuck on this for ages and tried loads of fixes but just can't get my head around it!
I run a site where the content of the pages are generated based upon a URL. For example:
http://www.mysite.com/http://www.supercheese.com
Would generate a mashup of content from mysite.com and supercheese.com
The .htaccess i use for this (at mysite.com) is:
<ifModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule .* - [L]
RewriteRule assets/.* - [L]
RewriteRule ^(.*)$ test.php?url=$1 [NC,L]
</ifModule>
So basically the second URL is passed in a php string.
My question is this, I need to remove the http:// from the address using .htaccess
E.G. If someone types:
http://www.mysite.com/http://www.supercheese.com
I need it to become:
http://www.mysite.com/www.supercheese.com
Many thanks in advance for taking a look at this. You guys rock.
You can simply use a RedirectMatch
http://httpd.apache.org/docs/2.0/mod/mod_alias.html#redirectmatch
Try with :
RedirectMatch ^/https?://(.*)$ http://www.mysite.com/$1
Edit : you have to put this before rewrite rules
Edit : add / before http
Edit : David is right, take a look a his answer and change your way of writing these urls
It looks to me like the url scheme is inherently problematic.
Using an unencoded colon : in the url - other than following the http to specify the access protocol - seems to make the server think that it is doing authentication of the form http://username:password#hostname/.
I know it doesn't directly answer the question, the solution is to change the url-scheme. ;-(

Categories